package leetcode.weekly.week306;

//Solution3Test
public class Solution3 {

	public String smallestNumber(String pattern) {
		char[] cs = pattern.toCharArray();
		int n = pattern.length();
		char[] ans = new char[n + 1];
		// IIIDIDDD
		// 123549876
		for (int i = 0; i < ans.length; i++) {
			ans[i] = (char) (i + 1 + '0');
		}
		for (int i = 0; i < n; i++) {
			char c = cs[i];
			if (c == 'D') {
				int t = i;
				while (t < n && cs[t] == 'D') {
					t++;
				}
				char cur = ans[t];
				ans[t] = ans[i];
				for (int j = i; j < t; j++) {
					ans[j] = cur;
					cur--;
				}
				i = t - 1;
			}
		}
		return new String(ans);
	}
}
